const defAttr = () => ({

    id: 1,//图形下标
    from: 0,
    to: 1,
    time: 1000,
    action: (p, data) => { },

    state: 0,//0:准备中 1:执行中 2:执行完毕
    beginTime: 0,
})

export default class Animate {
    constructor(attr) {
        // Object.assign(this, defAttr(), attr);

        this.animeArr = [];
    }

    add(data) {
        const { animeArr } = this;
        //如果存在动画
        for (let i = 0; i < animeArr.length; i++) {
            if (animeArr[i].id == data.id) {
                animeArr[i].from = data.from;
                animeArr[i].to = data.to;
                animeArr[i].state = 0;
                return;
            }
        }
        data.state = 0;
        animeArr.push(data);
    }

    update(time) {
        const { animeArr } = this;
        for (let i = 0; i < animeArr.length; i++) {
            let anime = animeArr[i];
            if (anime.state == 0) {
                anime.state = 1;
                anime.beginTime = time;
                // console.log(`动画[${anime.id}]开始执行`);
                // console.log('开始:' + animModelM4.elements[0])
            }

            if (anime.state == 1) {
                if (time > anime.beginTime + anime.time) {
                    anime.state = 2;
                } else {
                    //获取百分比
                    let a = (time - anime.beginTime) / anime.time;
                    anime.action(a, anime);
                }
            } else {
                //删除
                animeArr.splice(i, 1);
                // console.log(`动画[${anime.id}]执行完毕`);
                //强制归零
                anime.action(1, anime);
                // console.log('结束:' + animModelM4.elements[0])
                i--;
            }
        }
    }

}